package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.ViolationRecordDTO;
import com.eastfair.venueservice.dto.ViolationRecordPageQuery;
import com.eastfair.venueservice.dto.ViolationRecordSaveDTO;
import com.eastfair.venueservice.dto.ViolationRecordUpdateDTO;
import com.eastfair.venueservice.entity.ViolationRecord;
import com.eastfair.venueservice.service.ViolationRecordService;
import com.eastfair.venueservice.vo.ViolationRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 违规处罚记录
 * </p>
 *
 * @author lyw
 * @date 2022 -08-11
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/violationRecord")
@Api(value = "ViolationRecord", tags = "违规处罚记录")
@PreAuth(replace = "common:violationRecord:")
public class ViolationRecordController extends SuperController<ViolationRecordService, Long, ViolationRecord, ViolationRecordPageQuery, ViolationRecordSaveDTO, ViolationRecordUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ViolationRecordService violationRecordService;

    @Override
    public void handlerResult(IPage<ViolationRecord> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ViolationRecord> violationRecordList = list
                .stream()
                .map((map) -> ViolationRecord.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(violationRecordList));
    }

    @Override
    public void handlerQueryParams(PageParams<ViolationRecordPageQuery> params) {
        ViolationRecordPageQuery pageQuery = params.getModel();
        // 分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 查询违规惩罚记录列表分页
     *
     * @param params the params
     * @return r
     */
    @ApiOperation(value = "查询违规惩罚记录列表分页")
    @PostMapping(value = "/queryViolationRecordList")
    public R<Page<ViolationRecordVO>> queryViolationRecordList(@RequestBody PageParams<ViolationRecordDTO> params) {
        log.info("queryViolationRecordList - 查询违规惩罚记录列表分页, param={}", params);
        try {
            Page<ViolationRecordVO> page = violationRecordService.queryViolationRecordList(params);
            return R.success(page);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }


    /**
     * 根据被监督者的id查询数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据被监督者的id查询数据")
    @GetMapping(value = "/getViolationRecordListById/{id}")
    public R<List<ViolationRecordVO>> getViolationRecordListById(@PathVariable("id") Long id) {
        log.info("getViolationRecordListById - 查询违规惩罚记录列表分页, id={}", id);
        try {
            List<ViolationRecordVO> list = violationRecordService.getViolationRecordListById(id);
            return R.success(list);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }


    /**
     * 查询违规惩罚记录详情
     *
     * @param violationRecordDTO the violation record dto
     * @return r
     */
    @ApiOperation(value = "查询违规惩罚记录详情")
    @PostMapping(value = "/queryViolationRecordInfo")
    public R<ViolationRecordVO> getViolationRecord(@RequestBody ViolationRecordDTO violationRecordDTO) {
        log.info("queryViolationRecordInfo - 查询违规惩罚记录详情, param={}", violationRecordDTO);
        try {
            ViolationRecordVO violationRecordVO = violationRecordService.getViolationRecord(violationRecordDTO);
            return R.success(violationRecordVO);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 主场添加违规惩罚记录
     *
     * @param violationRecordDTO the violation record dto
     * @return r
     */
    @ApiOperation(value = "主场添加违规惩罚记录")
    @SysLog("主场添加违规惩罚记录")
    @PostMapping(value = "/saveViolationRecordInfo")
    public R<Boolean> saveForHome(@RequestBody ViolationRecordDTO violationRecordDTO) {
        log.info("saveForHome - 主场添加违规惩罚记录, violationRecordDTO={}", violationRecordDTO);
        try {
            return R.success(violationRecordService.saveForHome(violationRecordDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 场馆巡检生成违规惩罚记录.
     *
     * @param violationRecordDTO the violation record dto
     * @return the r
     */
    @SysLog("场馆巡检生成违规惩罚记录")
    @PostMapping(value = "/save/for/venue")
    public R<Boolean> saveForVenue(@RequestBody ViolationRecordDTO violationRecordDTO) {
        log.info("saveForVenue - 场馆巡检生成违规惩罚记录, violationRecordDTO={}", violationRecordDTO);
        return R.success(violationRecordService.saveForVenue(violationRecordDTO));
    }

    /**
     * 修改违规惩罚记录
     *
     * @param violationRecordDTO the violation record dto
     * @return r
     */
    @ApiOperation(value = "修改违规惩罚记录")
    @SysLog("修改违规记录")
    @PostMapping(value = "/updateViolationRecordInfo")
    public R<Boolean> updateViolationRecord(@RequestBody ViolationRecordDTO violationRecordDTO) {
        log.info("updateViolationRecord - 修改违规记录, violationRecordDTO={}", violationRecordDTO);
        try {
            return R.success(violationRecordService.updateViolationRecord(violationRecordDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 更新处罚扣款.
     *
     * @param violationRecordDTO the violation record dto
     * @return the r
     */
    @ApiOperation(value = "更新处罚扣款")
    @PostMapping(value = "/update/publishAmount")
    public R<Boolean> updatePublishAmount(@RequestBody ViolationRecordDTO violationRecordDTO) {
        log.info("updatePublishAmount - 更新处罚扣款, violationRecordDTO={}", violationRecordDTO);
        try {
            return R.successDef(violationRecordService.updateHomePunishAmount(violationRecordDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 撤销处罚.
     *
     * @param id the id
     * @return the r
     */
    @ApiOperation(value = "撤销处罚")
    @SysLog("撤销处罚")
    @GetMapping(value = "/update/cancel/{id}")
    public R<Boolean> updateViolationCancel(@PathVariable("id") Long id) {
        log.info("updateViolationCancel - 撤销处罚, id={}", id);
        try {
            return R.successDef(violationRecordService.cancelHomeViolationRecord(id));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 支付处罚扣款.
     *
     * @param violationRecordDTO the violation record dto
     * @return the r
     */
    @SysLog("支付处罚扣款")
    @PostMapping("/pay/punishAmount")
    public R<Boolean> payPunishAmount(@RequestBody ViolationRecordDTO violationRecordDTO) {
        log.info("payPunishAmount - 支付处罚扣款, violationRecordDTO={}", violationRecordDTO);
        return R.successDef(violationRecordService.payPunishAmount(violationRecordDTO));
    }

    /**
     * 删除违规惩罚记录
     *
     * @param id the id
     * @return r
     */
    @ApiOperation(value = "删除违规惩罚记录")
    @SysLog("删除处罚扣款")
    @GetMapping(value = "/delViolationRecordInfo/{id}")
    public R<Boolean> delViolationRecord(@PathVariable Long id) {
        log.info("delViolationRecord - 删除违规惩罚记录, id={}", id);
        try {
            return R.success(violationRecordService.delViolationRecord(id));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }
}